//
//  XKDrawMoneyController.m
//  xk100
//
//  Created by xiaokang on 2017/5/27.
//  Copyright © 2017年 xiaokang. All rights reserved.
//

#import "XKDrawMoneyController.h"
#import "XKBankCardManageController.h"
#import "XKBankDataModel.h"
#import "XKBankListModel.h"
#import "XKDrawMoneyPasswordView.h"
#import "XKDrawMoneyView.h"
@interface XKDrawMoneyController () <XKBankCardManageControllerDelegate, WCLPassWordViewDelegate>
@property (nonatomic, strong) XKDrawMoneyView *drawMoneyView;
@property (nonatomic, strong) XKDrawMoneyPasswordView *alertView;
@property (nonatomic, strong) UIView *alertBgView;
@property (nonatomic, strong) XKBankCardManageController *bankVC;
@property (nonatomic, copy) NSString *moneyCount;
@property (nonatomic, copy) NSString *card_id;;
@end

@implementation XKDrawMoneyController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initSubviews];
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    [self loadHeaderUserAssetData];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    _drawMoneyView.firstResponder = YES;
}


- (void)initSubviews {
    _drawMoneyView = [XKDrawMoneyView new];
    _drawMoneyView.feeString = [UserDefaults objectWithKey:@"withdraw_fee"];
    [self.view addSubview:_drawMoneyView];
    [_drawMoneyView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    
    
    _bankVC = [XKBankCardManageController new];
    _bankVC.delegate = self;
    
    @weakify(self);
    _drawMoneyView.SelectedBankCardBlock = ^void {
        @strongify(self);
        [self.navigationController pushViewController:self.bankVC animated:YES];
    };
    
    _drawMoneyView.DrawMoneyConfirmBlock = ^(NSString *moneyCount, NSString *card_id) {
        @strongify(self);
        self.moneyCount = moneyCount;
        self.card_id = card_id;
        [self showPasswordView];
    };
}

#pragma mark - XKBankCardManageControllerDelegate
- (void)advanceMoneyWithBankModel:(XKBankDataModel *)bankDataModel {
    if (bankDataModel) {
        _drawMoneyView.cardModel = bankDataModel;
    }
}


// 加载用户资产(提现时需要给用户提示可用余额)
- (void)loadHeaderUserAssetData {
    @weakify(self);
    [APIManager POSTRequestWith:APIPOSTRequestState_UserAssetData parameters:@{} successBlock:^(NSDictionary *successDict) {
        @strongify(self);
        NSString *availableMoney = [NSString stringWithFormat:@"%@",successDict[@"data"][@"user_currency_money"][@"rmbnum"]];
        if (IsEmptyStr(availableMoney)) {
            availableMoney = @"0.00";
        }
        self.drawMoneyView.availableBalanceString = availableMoney;
    } failureBlock:^(NSDictionary *failureDict) {
        
    } errorBlock:^(NSError *error) {
        
    }];
}








#pragma mark - 确认提现
- (void)showPasswordView {

    if (IsEmptyStr(_drawMoneyView.moneyCountString)) {
        [SVProgressHUD showInfoWithStatus:@"请输入提现金额!"];
        return;
    }

    if (_drawMoneyView.moneyCountString.doubleValue < kMaxAmountTwo.doubleValue) {
        [SVProgressHUD showInfoWithStatus:@"金额小于100元!"];
        return;
    } else if (_drawMoneyView.moneyCountString.doubleValue > _drawMoneyView.availableBalanceString.doubleValue) {
        [SVProgressHUD showInfoWithStatus:@"可用余额不足!"];
        return;
    }
    
    if (!_drawMoneyView.cardModel) {
        [SVProgressHUD showInfoWithStatus:@"请选择提现到账银行卡!"];
        return;
    }
    
    _alertView = [XKDrawMoneyPasswordView initView];
    _alertView.passwordInputView.delegate = self;
    _alertView.frame = CGRectMake((Main_Width-300)/2, (Main_Height-300)/2, 300, 120);
    _alertView.layer.cornerRadius = 8;
    _alertView.layer.masksToBounds = YES;
    _alertView.alpha = 0;
    _alertView.transform = CGAffineTransformScale(_alertView.transform, 0.9, 0.9);
    [_alertView.closeButton addTarget:self action:@selector(dismissPasswordAlertView:) forControlEvents:UIControlEventTouchUpInside];
    _alertBgView = [UIView new];
    _alertBgView.frame = [UIScreen mainScreen].bounds;
    _alertBgView.backgroundColor = BlackColor;
    _alertBgView.alpha = 0.7;
    
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [window addSubview:_alertBgView];
    [window addSubview:_alertView];
    
    [UIView animateWithDuration:0.25 delay:0 usingSpringWithDamping:0.7 initialSpringVelocity:50 options:UIViewAnimationOptionCurveEaseIn animations:^{
        _alertView.alpha = 1;
        _alertView.transform = CGAffineTransformIdentity;
    } completion:^(BOOL finished) {
        
    }];
}

#pragma mark - dismissPasswordAlertView
- (void)dismissPasswordAlertView:(UIButton *)sender {
    [_alertView removeFromSuperview];
    [_alertBgView removeFromSuperview];
    [UIView animateWithDuration:0.25 delay:0 usingSpringWithDamping:0.7 initialSpringVelocity:50 options:UIViewAnimationOptionCurveEaseOut animations:^{
        _alertView.alpha = 0;
        _alertView.transform = CGAffineTransformScale(_alertView.transform, 0.9, 0.9);
    } completion:^(BOOL finished) {
        
    }];
}

#pragma mark - WCLPassWordViewDelegate
- (void)passWordDidChange:(WCLPassWordView *)passWord {
    NSLog(@"密码输入改变%@",passWord.textStore);
}


- (void)passWordCompleteInput:(WCLPassWordView *)passWord {
    NSLog(@"密码输入完成%@",passWord.textStore);
    @weakify(self);
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        @strongify(self);
        [self dismissPasswordAlertView:nil];
        [self beganWithdrawWithMoney:_moneyCount bankCardID:_card_id tradePassword:passWord.textStore];
    });
}


- (void)passWordBeginInput:(WCLPassWordView *)passWord {
    NSLog(@"密码开始输入%@",passWord.textStore);
}


#pragma mark - 开始提现
- (void)beganWithdrawWithMoney:(NSString *)money bankCardID:(NSString *)bankCard_id tradePassword:(NSString *)tradePassword {
    [SVProgressHUD show];
    @weakify(self);
    [APIManager POSTRequestWith:APIPOSTRequestState_Withdraw parameters:@{@"money":money, @"bank_id":bankCard_id, @"pwdtrade":tradePassword} successBlock:^(NSDictionary *successDict) {
        @strongify(self);
        [SVProgressHUD showSuccessWithStatus:successDict[@"msg"]];
        self.drawMoneyView.moneyCountString = @"";
        self.drawMoneyView.actualCountSting = @"0.00";
        [self loadHeaderUserAssetData];
    } failureBlock:^(NSDictionary *failureDict) {
        [SVProgressHUD showErrorWithStatus:failureDict[@"msg"]];
    } errorBlock:^(NSError *error) {
        
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}







@end
